/*
 * Copyright (c) United Binary LLC.  All rights reserved.
 * 
 * This code is licensed under the MIT License
 * 
 * SEE: http://harnessit.codeplex.com/license
 * 
 */
using System;
using System.Diagnostics;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace UnitedBinary.Core.Windows.Forms.Stock
{
	/// <include file='Stock.xml' path='/Docs/TipOfTheDayForm/Class/*'/>
	public class TipOfTheDayForm : System.Windows.Forms.Form
	{
		#region Member Variables

		private System.Windows.Forms.CheckBox checkBoxShowAtStartup;
		private System.Windows.Forms.Button buttonNextTip;
		private System.Windows.Forms.Button buttonClose;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label labelTip;

		private bool loadAtStart = true;
		private string keyPath = "";
		private TipOfTheDay tipper = new TipOfTheDay();
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.ComponentModel.Container components = null;

		#endregion

		#region Construction and Cleanup

		/// <include file='Stock.xml' path='/Docs/TipOfTheDayForm/ctor_string/*'/>
		public TipOfTheDayForm(string appName)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// Add any constructor code after InitializeComponent call
			//
			keyPath = "Software\\" + appName + "\\TipOfTheDay";
			checkBoxShowAtStartup.Checked = LoadAtStart;
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if ( disposing )
			{
				if (components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}
		#endregion

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(TipOfTheDayForm));
			this.label1 = new System.Windows.Forms.Label();
			this.buttonNextTip = new System.Windows.Forms.Button();
			this.checkBoxShowAtStartup = new System.Windows.Forms.CheckBox();
			this.labelTip = new System.Windows.Forms.Label();
			this.panel1 = new System.Windows.Forms.Panel();
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.buttonClose = new System.Windows.Forms.Button();
			this.panel1.SuspendLayout();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			//
			// label1
			//
			this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label1.Location = new System.Drawing.Point(48, 24);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(144, 24);
			this.label1.TabIndex = 0;
			this.label1.Text = "Did You Know ...";
			//
			// buttonNextTip
			//
			this.buttonNextTip.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
			this.buttonNextTip.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.buttonNextTip.Location = new System.Drawing.Point(264, 248);
			this.buttonNextTip.Name = "buttonNextTip";
			this.buttonNextTip.TabIndex = 1;
			this.buttonNextTip.Text = "Next Tip";
			this.buttonNextTip.Click += new System.EventHandler(this.buttonNextTip_Click);
			//
			// checkBoxShowAtStartup
			//
			this.checkBoxShowAtStartup.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.checkBoxShowAtStartup.Checked = true;
			this.checkBoxShowAtStartup.CheckState = System.Windows.Forms.CheckState.Checked;
			this.checkBoxShowAtStartup.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.checkBoxShowAtStartup.Location = new System.Drawing.Point(24, 248);
			this.checkBoxShowAtStartup.Name = "checkBoxShowAtStartup";
			this.checkBoxShowAtStartup.Size = new System.Drawing.Size(128, 24);
			this.checkBoxShowAtStartup.TabIndex = 2;
			this.checkBoxShowAtStartup.Text = "Show Tips at Startup";
			this.checkBoxShowAtStartup.CheckedChanged += new System.EventHandler(this.checkBoxShowAtStartup_CheckedChanged);
			//
			// labelTip
			//
			this.labelTip.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
				| System.Windows.Forms.AnchorStyles.Left)
				| System.Windows.Forms.AnchorStyles.Right);
			this.labelTip.Location = new System.Drawing.Point(16, 80);
			this.labelTip.Name = "labelTip";
			this.labelTip.Size = new System.Drawing.Size(368, 112);
			this.labelTip.TabIndex = 1;
			this.labelTip.Text = "TIP GOES HERE";
			//
			// panel1
			//
			this.panel1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
				| System.Windows.Forms.AnchorStyles.Left)
				| System.Windows.Forms.AnchorStyles.Right);
			this.panel1.BackColor = System.Drawing.Color.White;
			this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {
																				 this.pictureBox1,
																				 this.labelTip,
																				 this.label1});
			this.panel1.Location = new System.Drawing.Point(8, 16);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(400, 208);
			this.panel1.TabIndex = 0;
			//
			// pictureBox1
			//
			this.pictureBox1.Image = ((System.Drawing.Bitmap)(resources.GetObject("pictureBox1.Image")));
			this.pictureBox1.Location = new System.Drawing.Point(8, 8);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new System.Drawing.Size(32, 48);
			this.pictureBox1.TabIndex = 2;
			this.pictureBox1.TabStop = false;
			//
			// groupBox1
			//
			this.groupBox1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
				| System.Windows.Forms.AnchorStyles.Left)
				| System.Windows.Forms.AnchorStyles.Right);
			this.groupBox1.BackColor = System.Drawing.SystemColors.Control;
			this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.panel1});
			this.groupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.groupBox1.Location = new System.Drawing.Point(8, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(416, 232);
			this.groupBox1.TabIndex = 3;
			this.groupBox1.TabStop = false;
			//
			// buttonClose
			//
			this.buttonClose.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
			this.buttonClose.DialogResult = System.Windows.Forms.DialogResult.OK;
			this.buttonClose.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.buttonClose.Location = new System.Drawing.Point(344, 248);
			this.buttonClose.Name = "buttonClose";
			this.buttonClose.TabIndex = 0;
			this.buttonClose.Text = "Close";
			//
			// TipOfTheDayForm
			//
			this.AcceptButton = this.buttonClose;
			this.AccessibleRole = System.Windows.Forms.AccessibleRole.Dialog;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.buttonClose;
			this.ClientSize = new System.Drawing.Size(432, 286);
			this.ControlBox = false;
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.groupBox1,
																		  this.buttonClose,
																		  this.buttonNextTip,
																		  this.checkBoxShowAtStartup});
			this.MinimumSize = new System.Drawing.Size(352, 275);
			this.Name = "TipOfTheDayForm";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "Tip of the Day";
			this.Load += new System.EventHandler(this.OnLoad);
			this.panel1.ResumeLayout(false);
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		#region Windows Forms Event Handlers

		private void buttonNextTip_Click(object sender, System.EventArgs e)
		{
			labelTip.Text = tipper.GetNextTip();
		}

		private void OnLoad(object sender, System.EventArgs e)
		{
			try
			{
				labelTip.Text = tipper.GetNextTip();
			}
			catch(Exception ex)
			{
				System.Diagnostics.Debug.WriteLine(ex.Message);
			}
		}

		private void checkBoxShowAtStartup_CheckedChanged(object sender, System.EventArgs e)
		{
			LoadAtStart = checkBoxShowAtStartup.Checked;
		}

		#endregion

		#region Tip Manipulation Code

		/// <include file='Stock.xml' path='/Docs/TipOfTheDayForm/AddTip/*'/>
		public void AddTip(string tip)
		{
			tipper.Add(tip);
		}

		/// <include file='Stock.xml' path='/Docs/TipOfTheDayForm/LoadAtStart/*'/>
		public bool LoadAtStart
		{
			get
			{
				try
				{
					Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(keyPath);
					Debug.WriteLine("Key=" + keyPath);
					if (key == null)
					{
						return true;
					}

					int val = (int)key.GetValue("ShowTip", 1);
					if (val == 0)
						loadAtStart = false;
					else
						loadAtStart = true;


					Debug.WriteLine("PersistentWindowState: Read LoadAtStart from registry: " + loadAtStart);
				}
				catch
				{
					Debug.WriteLine("PersistentWindowState Warning: Could not read registry key, ShowTip has never been set?");
					Debug.WriteLine("PersistentWindowState Warning: Using default value " + loadAtStart);
				}

				return loadAtStart;
			}
			set
			{
				int val = 0;
				if (value)
				{
					val = 1;
				}
				else
				{
					val = 0;
				}
				Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(keyPath);
				key.SetValue("ShowTip", val);
				Debug.WriteLine("PersistentValues: Wrote ShowTip to registry: " + value);
			}
		}

		/// <include file='Stock.xml' path='/Docs/TipOfTheDayForm/LoadTips_string/*'/>
		public void LoadTips(string file)
		{
			System.IO.StreamReader sr = null;

			try
			{
				sr = new System.IO.StreamReader(file);

				tipper.Clear();
				string tip = "";
				while (tip != null)
				{
					tip = sr.ReadLine();
					if (tip != null && tip.Length > 0)
					{
						if (tip.StartsWith("\\f"))
						{
							tip = tip.Replace("\\f", "");
							tip = tip.Replace("\\n", "\n");
							tip = tip.Replace("\\t", "\t");
						}
						tipper.Add(tip);
					}
				}
			}
			finally
			{
				if (sr != null)
				{
					sr.Close();
				}
			}
		}

		/// <include file='Stock.xml' path='/Docs/TipOfTheDayForm/LoadTips_string_array/*'/>
		public void LoadTips(string[] tips)
		{
			tipper.Clear();
			foreach (string tip in tips)
			{
				if (tip != null && tip.Length > 0)
				{
					string newTip = tip;
					if (tip.StartsWith("\\f"))
					{
						newTip = newTip.Replace("\\f", "");
						newTip = newTip.Replace("\\n", "\n");
						newTip = newTip.Replace("\\t", "\t");
					}
					tipper.Add(newTip);
				}
			}
		}

		#endregion
	}
}
